﻿/************************************
【功能说明】 
分析从Atlas.ACDC.Graph表中的GraphValues数据。
经初步分析，一个测量结果会对应两条曲线，分别是扭距和角度（弧度制）
在以下代码中使用 torques 和 angles 表示。 

【更新记录】
2022/04/12 初步完成出结果
2022/04/14 编写此类，并添加结论
 
**************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Verifications.vers
{
    internal class AtlasGraphDemo
    {
        string torques = "84A8233D6024DC3CE858023D102D3F3CB8A8CD3C6024DC3CC035223CCC610F3D00000000B8A8CD3CB8A8CD3C80BAE73AF823883C000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000A09F163DA050733CF823883CC035A23B2836F63CA050733C6024DC3CC035A23BF823883C102D3F3C102D3F3C6024DC3C481BA53C6024DC3CC035223CC035A23B6024DCBB84A8233DF823883C481BA53C2836F63C00000000B8A8CD3C68B1B03CE858023D6024DC3C481BA53CC035A23B58E62A3DC873533DA050733C481BA53C102D3F3D84A8233D3CEF373D6024DC3C56EFCC3DAAD4253EFC737D3DEF42163E931F0C3E2836763D2836763D1E500A3E2C24B23DE46AC63D70EFE13D9EA8B83D5ADD883D102D3F3D12249D3D70EFE13D4285AE3DFC737D3D5ADD883D9CB1DA3D703E853DF8D4E43D6F9B443E4E078D3EE60C9D3FD79118401A0B39400FDC534036548140FA728240D8923C40F52F224026E34240B21257400D374B4017C03D4086F73A400E353D40385143400B924240621D48406A004840CC644140E3C24540BC9B4940EA024B40E46A4640C6CC3C40B94E4040B06B404059383A40F48B3D40020742401A0B39409CB0364041344340112A4740C6CC3C40D8923C40AD204540413443401D5842404E0A3F40E60D41401D5842407BC93F40D8923C40E11D3D40C8714540112A4740B06B4040815F36405D2B36403EE739400B92424093CF4440B06B4040413443401A0D4740E3C24540961A40404134434050AF4740447F3E4059383A40BD413C4059383A40879F3B407BC93F40F0984140E7B54140C08E4540961A4040292E3E4059383A40B94E4040BFE6444038A942407A213F40F098414083AC3F40E0753C40D8923C40A14A494092274440E4683840B06B4040BFE6444052FA42400BEA41400207424005544B40112A47400BEA41401082464089444440ABD33B4051553A40FC6E3D40D5474140C6243C4036043A40685B3F4010824640F23D4A401D584240C08E4540BFE64440F53046405FD03E40CFAD2E40787C364093CF4440C08E4540253B42400EDD3D404D623E4060783F4026E342409EFD3F4074893A40F48B3D4075313B405FD03E40B3B84940CE094A40A4954440E60D4140C08E45401A0D474089444440ABD33B4000623940EEF33840094539405C8543403851434024963940ABD33B40292E3E40B06B40405C85434086F94840C08E454053A24340BC9B494053A2434047CC4740621D48400D374B403AF64B4053A2434002074240112A4740F0984140E11D3D4017C03D40A53D4540894444400E353D40020742402C7B47407A213F4017C03D40292E3E400BEA4140F23D4A401D584240FFB938401A0B39404D623E407BC93F40A2F03B403EE739400B924240B06B4040E11D3D408944444044814C40A14A49405C854340A2F03B40ABD33B40253B42402C7B4740DADF45409EFD3F40F53046409867494038A942407A213F4032113E4002074240D54741400E353D4093CD3640089D3840F48B3D40B06B4040685B3F400EDD3D4060783F40BD413C4032113E407A213F40B94E4040E0753C401BB339403EE739401A0B3940C6CC3C401A0D47406BA84840FE13464093CF4440AD20454050AF4740447F3E4090823B40355E474060783F40660E364086F73A404E0A3F40961A4040447F3E40E468384009453940FFB93840660E364074893A405C854340879F3B4059383A4033B93E40F48B3D409EFD3F40961A404002074240A53D454059383A40BD413C406BA84840BFE6444038A94240D5474140D54741407A213F405D2B3640E4683840AFC637409CB0364093CD3640B7A93740447F3E403EE739404D623E401082464083AC3F40089D38403EE7394090823B40BD413C4094753740FD143040C6243C40B94E4040660E36409CB03640C91738409475374000623940D8923C407BC93F4038514340F0984140B06B4040BD413C40D6ED3340660E3640FC6E3D4032113E404D623E4085514840385143409CB036409C58374075313B40C6243C400EDD3D401BB339405FD03E409EFD3F40FC6E3D4060783F40BAF64040961A4040B06B404033B93E400E353D407A213F4051553A40D8923C40B94E4040BD413C40D8923C401D584240922744404E0A3F407A213F404D623E4000623940A4954440355E4740385143407BC93F40CBBC4040BD413C405AE03A40C6243C40F3E33C401D584240A2F03B400945394084523240DFD0334026E34240BC9B494075313B408D3532408207374036043A40879F3B40C91738400EDD3D409EFD3F40D8923C40D6ED334051553A409FA540407BC93F4060783F40B06B40400B92424032113E4074893A4060783F404E0A3F4032113E407BC93F40F0984140961A404052FA424032113E40AFC637407A213F40112A47401D584240E11D3D40C6243C40C6CC3C40961A4040F48B3D4090823B40BD413C4033B93E40BD413C40879F3B40879F3B40BAF64040820737404E0831400EDD3D409EFD3F400EDD3D4060783F4060783F40E60D4140894444405FD03E40C6CC3C4059383A40ED4B38409475374059383A404D623E407BC93F40447F3E40447F3E40292E3E40089D384024963940EEF33840EEF33840C6243C401A0B3940447F3E40447F3E40EEF338400E353D4060783F401A0B3940BD413C40D8923C40B06B4040413443404BBD3540BAF432401D5842405894554099665F40E7B66540316E7D404E5B8D403D939C401619A740B210AC4045D2B740713FC640C5D3D040035AD8408A98E1402C28EB40C288F240E079F5409D59F84055F6FD40590A01413E8F0041AA2FF4407141F140AA83F440F82AF3405F7FF0400555EF402989EF401A0EEB404EB8E940BDF0ED4041E2ED40C3DBEB404235E74027E4E64027E4E6404235E740B1C0E440695CE3400B3FE6407EC2E540BAF7E44047CCE4407BCAE340961BE440B757E2400E8AE140301AE040AC28E04018C1E1408A98E1403366E240C08EE240E21EE14051AADE4075DEDE404B6BE040ABD4DF40E8B5DE40A9DCDD405156DE40DFD2DE400E8AE1404D64E9400555EF408C92F14086A7F340D103F740B367FB4088A0FC401F00FD40A82F00418F0002414AE802411E4F034132B90441F5010641832A06414E840641A8020841BA460841C37D0841879C0941A2170A41B4850A41767E0A41CF000B4142AE0A416E710A410CB80941990A0A41E01E0A4142AE0A41BD920A4189EC0A41F3880B41DA5D0C412A7F0C41C9150D41B9F70D41FF0B0E41C22E0E41C32A0F41E7DC0F41149C10412830124155EF12414DDE134194F213413DC01441B0931541954215417D131741D66B1741DFA2174164BE1741C427174126B7174194C91A4147F41C41BF621A41896C19416E1B1941D9E11941E0C8184149431741887018413C6D1B4135861C41CABF1B412B4F1C418CB81B4135861C41DF791E4125641E41FACA1E4140DF1E4177FF1F41C87021416FEE204139F81F41ECF82141736024417C97244137A925415C57274105FB27414452294146782A41A6E12941A6E1294123C22A418D8C2A414F852A413B1B2941822F2941A6E1294108472A41FF392A4171E72941EDF52941822F2941A6B7294161C92A41E70A2C41745D2C413E672B4159E22B41AA532D41C7CA2E4180B62E4142D92E4129842F41A68E3041FDEA2F41F9782C41653B2E41D8122E41CDB52C41F94E2C41408D2C4124122C4159B82B4123EC2A41DCD72A41F7282B414F852A4146782A4108712A41A6B729413B1B294155462841BE102841D865274141DC26416D75264152FA25410BE62541634225418EDB24418EDB2441BA7424414FAE2341F65523413433234119B822418B8F2241792122419CD72141792122414355214128DA2041E19B20418036204154732041ABCF1F415B5A1F4130EB1F41A2981F41FACA1E412E9B1E41E8861E415A341E410A131E41F8CE1D413FE31D41EF971D41";
        string angles = "0015CFBBE0AE923C000000000015CF3B0015CFBB0015CF3B0015CF3B0015CFBB000000000015CFBB000000000015CFBB0015CFBB000000000000000000000000000000000015CFBB0015CFBB0015CF3B000000000015CF3B0015CFBB0015CFBB000000000000000000000000000000000015CFBB0015CF3B0015CF3B000000000015CF3B000000000015CF3B0000000000000000000000000015CF3B0015CFBB0015CFBB0015CF3B0015CF3B0015CF3B0015CFBB0000000040D33DBC000000000015CFBB0000000000000000000000000015CF3B0015CF3B0015CF3B0015CF3B0015CFBB0000000040D33D3C000000000015CFBB000000000015CFBBB023423D000E0A3E98617C3ECC37C33E5E89173F9A03553FF4808E3FBCB0BD3FB8C0F33FD42117409CFB3940F87E62404EAC8540CFAB9A40AED0B040ECECC640D99BDD40CAEBF640ED790741C25F13414F581E41E395294125663541F30641416A5E4C41E0B55741E2DD6241FAA66D413C777941B1B08241B2448841F85F8A41C9188A41D80B8A4148FB8A415A5C8B41CE5889411DF4894106818B41E52C8B413E488A415E9C8A41C5D88A415E9C8A41C5D88A4119B48A4128A78A4119B48A41A4848A418EE38A41D3CB8A4128A78A4119B48A4119B48A410AC18A41D3CB8A4128A78A41C5D88A41D3CB8A4119B48A4119B48A417FF08A418EE38A41A4848A4128A78A41C5D88A4128A78A4119B48A410AC18A4119B48A4119B48A410AC18A41D3CB8A410AC18A418EE38A41C5D88A4119B48A416D8F8A416D8F8A41D3CB8A41C5D88A4119B48A41A4848A41C5D88A4119B48A415E9C8A4128A78A4128A78A418EE38A4119B48A416D8F8A4148FB8A416D8F8A418EE38A4119B48A41C5D88A410AC18A418EE38A410AC18A41D3CB8A415E9C8A41C5D88A418EE38A4119B48A4119B48A417FF08A41D3CB8A41C5D88A41C5D88A417FF08A410AC18A41B3778A4139088B41C5D88A4119B48A410AC18A4119B48A417FF08A4128A78A410AC18A41C5D88A41D3CB8A416D8F8A41D3CB8A410AC18A410AC18A41D3CB8A4119B48A41C5D88A410AC18A41C5D88A4128A78A410AC18A41C5D88A415E9C8A418EE38A4107538A4128A78A41E52C8B410AC18A410AC18A4119B48A41C5D88A4119B48A4119B48A41C5D88A416D8F8A41D3CB8A41D3CB8A41D3CB8A418EE38A41C5D88A4128A78A410AC18A41C5D88A410AC18A4128A78A4119B48A415E9C8A410AC18A41D3CB8A410AC18A41C5D88A415E9C8A4128A78A41D3CB8A410AC18A41D3CB8A41C5D88A415E9C8A4119B48A417FF08A415E9C8A410AC18A417FF08A416D8F8A410AC18A416D8F8A4128A78A410AC18A41A4848A416D8F8A410AC18A410AC18A416D8F8A415E9C8A410AC18A41D3CB8A416D8F8A4128A78A418EE38A4128A78A418EE38A4128A78A415E9C8A41D3CB8A418EE38A41D3CB8A410AC18A4128A78A418EE38A41C5D88A4128A78A41C5D88A418EE38A4119B48A4128A78A4119B48A4119B48A41C5D88A41C5D88A418EE38A410AC18A41D3CB8A418EE38A415E9C8A41D3CB8A41D3CB8A41C5D88A41D3CB8A415E9C8A41D3CB8A41D3CB8A417FF08A41D3CB8A418EE38A410AC18A410AC18A41D3CB8A410AC18A41C5D88A418EE38A4119B48A410AC18A41C5D88A417FF08A41D3CB8A4139088B410AC18A415E9C8A41C5D88A41C5D88A41C5D88A41D3CB8A4119B48A41E52C8B410AC18A415E9C8A417FF08A417FF08A41D3CB8A418EE38A4119B48A418EE38A41C5D88A418EE38A417FF08A4102138B410AC18A410AC18A4139088B41C5D88A418EE38A418EE38A418EE38A418EE38A410AC18A410AC18A41AE378B4119B48A4148FB8A4148FB8A418EE38A418EE38A41D3CB8A4139088B417FF08A417FF08A4102138B418EE38A41F41F8B418EE38A4139088B41A0448B417FF08A41C5D88A4102138B4102138B4148FB8A418EE38A41C5D88A4102138B41AE378B418EE38A41E52C8B41F41F8B4139088B4148FB8A41AE378B41F41F8B41F41F8B4148FB8A417FF08A4102138B41C5D88A4148FB8A415A5C8B4102138B417FF08A41A0448B4148FB8A4119B48A4139088B41E52C8B4139088B41E52C8B4102138B4139088B41AE378B4148FB8A41AE378B41E52C8B41F41F8B417FF08A41E52C8B4139088B4148FB8A4148FB8A41AE378B41F41F8B4102138B417FF08A41AE378B41E52C8B41F41F8B41F41F8B41E52C8B4148FB8A41A0448B41E52C8B4139088B4148FB8A41AE378B4139088B4102138B4102138B41F41F8B41A0448B4139088B4148FB8A4139088B4148FB8A4114748B415A5C8B41C5D88A418EE38A41A0448B4102138B41E52C8B4139088B415A5C8B41AE378B41E52C8B417FF08A41A0448B41E52C8B41F41F8B41E52C8B41AE378B4148FB8A41E52C8B4102138B41F41F8B41E52C8B4102138B41F41F8B41E52C8B41F41F8B41AE378B41E52C8B4148FB8A41F41F8B41A0448B41F41F8B4139088B41E52C8B41AE378B41F41F8B41AE378B41F41F8B41E52C8B4102138B4139088B41F41F8B41E52C8B41AE378B4139088B4102138B4123678B41E52C8B4102138B41AE378B41AE378B41AE378B41A0448B4102138B41F41F8B41F41F8B41F41F8B41E52C8B41E52C8B41AE378B41AE378B41AE378B41F41F8B41694F8B4148FB8A41A0448B41AE378B41AE378B415A5C8B41F41F8B415A5C8B41E52C8B4102138B41AE378B41694F8B4102138B41694F8B415A5C8B41E52C8B41A0448B4139088B4102138B4123678B4114748B415A5C8B41A0448B4106818B4135C88B41640F8C4101418C41CA4B8C4176708C41FD008D413A3B8D41E65F8D41C1CB8D419C378E4131BB8E41FE338F41B40B904106159141A64B9241F1A69341ED669441AAEC9441B49F95410CE99541043B96415C8496417CD89641E214974157449741FBBA9741ECC797411C0F9841FE289841F67A98412D7098415DB798414EC49841C3F398416E189941293099419E5F99418F6C9941128F994179CB9941BEB3994179CB994124F09941D0149A4154379A4145449A41FF5B9A41F1689A41AB809A4165989A4165989A4195DF9A41CCD49A41DAC79A41CCD49A414FF79A41EC289B41A6409B41D6879B4181AC9B41F6DB9B41083D9C41F29B9C41BE149D41628B9D41E91B9E419EF39E41EE8E9F412144A041C828A141470BA241EEEFA241E9AFA34114B7A441F290A541D7AFA6417E94A7419AA8A841FCA4A9416C94AA4188A8AB41F997AC41A07CAD411F5FAE411A1FAF4116DFAF414894B0410D5FB141CAE4B141C6A4B241D057B34120F3B341FECCB4410880B5411233B641B917B741FABFB741A1A4B841577CB9411C47BA41B438BB412528BC41CC0CBD41E820BE415810BF418317C041E413C1410F1BC2416224C3417E38C441A93FC541192FC64106FCC641ADE0C741EA48C941065DCA411010CB4180FFCB41B2B4CC410250CD41CEC8CD41CA88CE410B31CF415BCCCF411F97D0410C64D1415CFFD141E28FD241CF5CD3414E3FD4410CC5D441C29CD5418667D6413C3FD74137FFD741EDD6D841CBB0D941F6B7DA41E384DB41FF98DC41D5C4DD4154A7DE410DEDDF419E30E141F87EE2419070E341679CE44192A3E541F39FE641638FE7410B74E8410634E941AD18EA41A9D8EA4128BBEB410695EC415630ED4197D8ED415BA3EE414870EF414330F041F907F141E6D4F14165B7F2411B8FF3417D8BF441784BF541113DF641812CF741E228F841869FF8412E84F941E45BFA41BEC7FA410E63FB414118FC410D91FC41E8FCFC417D80FD41CD1BFE41DF7CFE417400FF414F6CFF4170C0FF418A1B00429A450042466A00425694004243CA004277E8004296050142412A0142FC4101422F600142627E0142819B0142B4B90142E7D701423DEA0142F70102424D14024280320242C2430242185602424B7402428C8502428C85024238AA02427ABB0242D0CD024248D402428AE50242E0F7024259FE0242360A034269280342AB3903424634034223400342DE570342565E0342986F0342897C0342";


        public void Test()
        {
            showCurveString();

            var v = "AD741E41";

            Console.WriteLine(Convert0(v));
            Console.WriteLine(Convert1(v));

            var v0 = BytesToFloat(HexStringToBytes(v));
            var vr = BytesToFloat(HexStringToBytes(v, true));
            Console.WriteLine($"v0 = {v0}, vr = {vr}");
        }

        public void showCurveString()
        {
            int len = torques.Length / 8;
            for (int i = 0; i < len; i++)
            {
                Console.WriteLine(Convert1(torques.Substring(i * 8, 8)) + "    " + Convert1(angles.Substring(i * 8, 8)));
            }
            Console.WriteLine("Len: " + len);
        }

        public static double Convert0(string v)
        {
            var bts = new byte[4];
            for (int i = 0; i < bts.Length; i++)
                bts[bts.Length - 1 - i] = System.Convert.ToByte(v.Substring(i * 2, 2), 16);

            MemoryStream ms = new MemoryStream(bts);
            BinaryReader br = new BinaryReader(ms);
            var d1 = br.ReadSingle();

            Console.WriteLine($"d1 = {d1}");
            br.Close();
            return d1;
        }


        public static double Convert1(string v)
        {
            var bts = new byte[4];
            for (int i = 0; i < bts.Length; i++)
                bts[i] = System.Convert.ToByte(v.Substring(i * 2, 2), 16);

            MemoryStream ms = new MemoryStream(bts);
            BinaryReader br = new BinaryReader(ms);
            var d1 = br.ReadSingle();

            Console.WriteLine($"d1 = {d1}");
            br.Close();
            return d1;
        }


        public static byte[] HexStringToBytes(string hexString, bool reverseOrder = false)
        {
            var bts = new byte[hexString.Length / 2];
            for (int i = 0; i < bts.Length; i++)
                bts[reverseOrder ? bts.Length - i - 1 : i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return bts;
        }

        public static float BytesToFloat(byte[] bts)
        {
            float f = float.NaN;
            using (BinaryReader br = new BinaryReader(new MemoryStream(bts)))
                f = br.ReadSingle();
            return f;
        }
    }
}
